//
//  LYTabBarController.m
//  artist
//
//  Created by Jay_Apple on 16/2/29.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYTabBarController.h"
#import "LYNavigationController.h"
#import "LYHomeViewController.h"
#import "LYMineViewController.h"
#import "LYDiscoverViewController.h"
#import "LYSubjectViewController.h"
#import "LYTabBar.h"
#import "UIImage+Image.h"

@interface LYTabBarController ()

@end

@implementation LYTabBarController

+ (void)initialize
{
    // 通过appearance统一设置所有UITabBarItem的文字属性
    // 后面带有UI_APPEARANCE_SELECTOR的方法, 都可以通过appearance对象来统一设置
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:LYTabBarTextFont];
    attrs[NSForegroundColorAttributeName] = [UIColor colorWithHexString:LYTabBarTextColor alpha:1.0];
    
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSFontAttributeName] = attrs[NSFontAttributeName];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor colorWithHexString:LYTabBarTextSelectedColor alpha:1.0];
    
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    
    
            UIImage *bgImage = [UIImage imageWithColor:[UIColor colorWithHexString:LYTabBarBGColor alpha:1.0]];
            UIImage *shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
            [[UITabBar appearance] setBackgroundImage:bgImage];
            [[UITabBar appearance] setShadowImage:shadowImage];
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 添加子控制器
    [self setUpChildVCs];
    
    // 更换tabBar
    [self setValue:[[LYTabBar alloc] init] forKeyPath:@"tabBar"];
}

/**
 *  初始化
 */
- (void)setUpChildVCs{

    [self setupChildVc:[[LYHomeViewController alloc] init] title:@"首页" image:@"首页未选中" selectedImage:@"首页选中"];
    
    [self setupChildVc:[[LYSubjectViewController alloc] init] title:@"科目" image:@"科目未选中" selectedImage:@"科目选中"];
    
    [self setupChildVc:[[LYDiscoverViewController alloc] init] title:@"发现" image:@"发现未选中" selectedImage:@"发现选中"];
    
    [self setupChildVc:[[LYMineViewController alloc] init] title:@"我" image:@"我的未选中" selectedImage:@"我的选中"];

}

/**
 * 初始化子控制器
 */
- (void)setupChildVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置文字和图片
    vc.navigationItem.title = title;
    vc.tabBarItem.title = title;
    vc.tabBarItem.image = [UIImage imageNamed:image];
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    
    // 包装一个导航控制器, 添加导航控制器为tabbarcontroller的子控制器
    LYNavigationController *nav = [[LYNavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:nav];
}
@end
